/**
 * @filename:YouXiangServiceImpl Jan 9, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.db.DbUtil;
import com.wonder.app.domain.YouXiang;
import com.wonder.app.mapper.YouXiangDao;
import com.wonder.app.service.YouXiangService;
import com.wonder.app.request.YouXiangRequest;
import com.wonder.common.utils.MessageUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**   
 * @Description:TODO(邮箱管理服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class YouXiangServiceImpl  extends ServiceImpl<YouXiangDao, YouXiang> implements YouXiangService  {


	@Autowired
	private YouXiangService youXiangService;
	@Autowired
	private YouXiangDao youXiangDao;
    /**
     * 验证邮箱管理信息
     * 
     * @param youXiang
     * @param type
     * @return String
     */
    @Override
    public String validate(YouXiang  youXiang,String type){
    
       if(youXiang.getPromoterMail()==null) return "发起人邮箱"+MessageUtils.message("no.null");	
    
       return "";
    }
    
    
    /**
	 * Description:邮箱管理分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<YouXiang>   //分页记录
	 */
	@Override 
	public IPage<YouXiang> selectList(YouXiangRequest request, Page<YouXiang> page){
	    LambdaQueryWrapper<YouXiang> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getFid()) {
	       lambdaQueryWrapper.eq(YouXiang::getFid, request.getFid());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(YouXiang::getFid);	
		Page<YouXiang> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	/**
	 * @param addressee 收件人
	 * @param text      内容
	 * @param title     标题
	 * @param host      邮箱服务器
	 * @param user      发件人账号
	 * @param password  发件人授权码
	 * @return
	 */
	//发送邮件
	public boolean sendMail(String addressee , String text , String title , String host , String user , String password){
		try {

			//邮件主题
			// String title = entity.getTheme();
			// 邮件内容
			// String text = entity.getTheme();
			//收件人地址
			//String addressee = entity.getPromoterMail();

			//创建Properties集合
			final Properties pros = new Properties();
			pros.put("mail.smtp.auth","true");          // 用户认证
			pros.put("mail.smtp.host",host);   // 主机名      这里是qq邮箱  如果是163  就把qq改为163
			// pros.put("mail.smtp.host","smtp.qq.com");   // 主机名      这里是qq邮箱  如果是163  就把qq改为163

			// 发件人的账号
			pros.put("mail.user",user);
			//pros.put("mail.user","2868886485@qq.com");
			// 发件人的授权码
			pros.put("mail.password",password);
			//pros.put("mail.password","wbcblsehxrvgddei");

			// 构建授权码信息  用于进行SMTP身份验证
			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					//用户名 密码（授权码）
					String userName = pros.getProperty("mail.user");
					String password = pros.getProperty("mail.password");
					return new PasswordAuthentication(userName,password);
				}
			};

			// 创建邮件会话
			Session session = Session.getInstance(pros, authenticator);
			// 创建邮件信息
			MimeMessage message = new MimeMessage(session);
			// 创建发件人
			String name = pros.getProperty("mail.user");
			InternetAddress address = new InternetAddress(name);
			message.setFrom(address);

			// 设置收件人
			InternetAddress ToAddressee = new InternetAddress(addressee);
			//InternetAddress ToAddressee = new InternetAddress("2074832661@qq.com");
			message.setRecipient(Message.RecipientType.TO,ToAddressee);

			// 设置邮件标题
			message.setSubject(title);

			// 设置邮件内容
			message.setContent(text,"text/html;charset=UTF-8");

			message.setText("邮件内容", "UTF-8");
			// message.setContent("csshishuju","text/html;charset=UTF-8");

			// 发送邮件
			Transport.send(message);

			//charushuju(addressee , text , title ,  user );

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

//	public boolean  charushuju (String addressee , String text , String title , String fuser ) throws SQLException {
//		String url = "jdbc:mysql://localhost:3306/boxing";
//		String user = "root";
//		String password = "123456";
//		Connection conn = DriverManager.getConnection(url, user, password);
//		Statement stmt = conn.createStatement();
//		String sql = "INSERT INTO `表名` (`字段1`, `字段2`, ...) VALUES ('值1', '值2', ...)";
//		stmt.executeUpdate(sql);
//		return true;
//	}


	//提交
	@Override
	public boolean youXiangTiJiao(Long fid) {
		YouXiang  youXiang = youXiangDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = youXiang.getTijiao();
		boolean sh = youXiang.getShenhe();
		boolean res ;

		if(tj == true ){
			res = false;
		}else{
			youXiang.setTijiao(true);
			res = youXiangService.updateById(youXiang);
		}
		return res;
	}

	//撤销
	@Override
	public boolean youXiangCheXiao(Long fid) {
		YouXiang  youXiang = youXiangDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = youXiang.getTijiao();
		boolean sh = youXiang.getShenhe();
		boolean res ;
		if(tj == true && sh == false){
			youXiang.setTijiao(false);
			res = youXiangService.updateById(youXiang);
			//true;
		}else{
			res = false ;
		}

		return res;
	}

	//审核
	@Override
	public boolean youXiangShenHe(Long fid) {
		YouXiang  youXiang = youXiangDao.selectById(fid);
		//获取提交和审核的状态
		boolean tj = youXiang.getTijiao();
		boolean sh = youXiang.getShenhe();
		boolean res ;
		Date currentDate = new Date();

		//当提交和审核状态是 true的时候，停止审核操作
		if(tj == true && sh == true  ){
			res = false;
		}
		else if(tj == true && sh == false ){
			youXiang.setShenhe(true);
			youXiang.setAuditDate(currentDate);
			res = youXiangService.updateById(youXiang);
		}
		else{
			res = false;
		}
		return res;
	}

	//反审核
	@Override
	public boolean youXiangFanShenHe(Long fid) {
		YouXiang  youXiang = youXiangDao.selectById(fid);
		//获取提交和审核的状态
		boolean tj = youXiang.getTijiao();
		boolean sh = youXiang.getShenhe();
		boolean res ;

		if(tj == true && sh == true ){
			youXiang.setTijiao(false);
			youXiang.setShenhe(false);
			youXiangService.updateById(youXiang);
			res = true;
		}else {
			res = false;
		}
		return res;
	}

}